﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Undsf.RmmvDbms.Data;

namespace Undsf.RmmvDbms
{
    class RmmvProject
    {
        public const int SUCCESS = 0;
        public const int ERROR_PROJECT_DIR_NOT_EXISTS = 1;
        public const int ERROR_DATA_DIR_NOT_EXISTS = 2;

        public string Path { get; set; }

        public IDictionary<int, Actor> Actors { get; set; }
        public IDictionary<int, Class> Classes { get; set; }
        public IDictionary<int, Skill> Skills { get; set; }
        public IDictionary<int, Item> Items { get; set; }
        public IDictionary<int, Weapon> Weapons { get; set; }
        public IDictionary<int, Armor> Armors { get; set; }
        public IDictionary<int, Enemy> Enemies { get; set; }
        public IDictionary<int, Troop> Troops { get; set; }
        public IDictionary<int, State> States { get; set; }
        public IDictionary<int, Animation> Animas { get; set; }
        public IDictionary<int, Tileset> Tilesets { get; set; }
        public IDictionary<int, CommonEvent> Events { get; set; }
        public Data.System System { get; set; }

        public RmmvProject(string path = null)
        {
            if (path != null)
            {
                Load(path);
            }
        }

        public int Load(string path)
        {
            int result = SUCCESS;
            do
            {
                if (Directory.Exists(path) == false)
                {
                    return ERROR_PROJECT_DIR_NOT_EXISTS;
                }
                if (Directory.Exists(path+"/data") == false)
                {
                    return ERROR_DATA_DIR_NOT_EXISTS;
                }

                Path = path;

                result = LoadActors();
                if (result != SUCCESS) break;

                result = LoadClasses();
                if (result != SUCCESS) break;

                result = LoadWeapons();
                if (result != SUCCESS) break;

                result = LoadArmors();
                if (result != SUCCESS) break;
            } while (false);
            return result;
        }

        public int LoadActors()
        {
            string filePath = Path + "/data/Actors.json";
            string content = File.ReadAllText(filePath);
            List<Actor> actorList = JsonConvert.DeserializeObject<List<Actor>>(content);
            Actors = new Dictionary<int, Actor>();
            foreach (Actor actor in actorList)
            {
                if (actor == null) continue;
                Actors[actor.id] = actor;
            }
            return SUCCESS;
        }

        public int LoadClasses()
        {
            string filePath = Path + "/data/Classes.json";
            string content = File.ReadAllText(filePath);
            List<Class> classList = JsonConvert.DeserializeObject<List<Class>>(content);
            Classes = new Dictionary<int, Class>();
            foreach (Class clazz in classList)
            {
                if (clazz == null) continue;
                Classes[clazz.id] = clazz;
            }
            return SUCCESS;
        }

        public int LoadWeapons()
        {
            string filePath = Path + "/data/Weapons.json";
            string content = File.ReadAllText(filePath);
            List<Weapon> weaponList = JsonConvert.DeserializeObject<List<Weapon>>(content);
            Weapons = new Dictionary<int, Weapon>();
            foreach (Weapon weapon in weaponList)
            {
                if (weapon == null) continue;
                Weapons[weapon.id] = weapon;
            }
            return SUCCESS;
        }

        public int LoadArmors()
        {
            string filePath = Path + "/data/Armors.json";
            string content = File.ReadAllText(filePath);
            List<Armor> armorList = JsonConvert.DeserializeObject<List<Armor>>(content);
            Armors = new Dictionary<int, Armor>();
            foreach (Armor armor in armorList)
            {
                if (armor == null) continue;
                Armors[armor.id] = armor;
            }
            return SUCCESS;
        }

        public bool Save()
        {
            return SaveAs(Path);
        }

        public bool SaveAs(string path)
        {
            return false;
        }
    }
}
